<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
date_default_timezone_set ( 'prc' );
class Home extends CI_Controller{
	public function __construct() {
		parent::__construct ();
	}
	/*
	 * 返回主页
	 */
	public function index(){
		$userid = $this->session->userdata ( 'id' );
		if (empty($userid)) {
			redirect ( 'passport/index' );
		}
		$this->load->model ( 'user_model' );
		$user = $this->user_model->load($userid);
		$data['total'] =$this->get_amount($userid);
		$data['date']= $this->common->getOrderDate($user['school']);
		$data['order']=$user['order_state'];//下单状态
		$this->load->model ( 'messagerecord_model' );
		$data['message']=$this->messagerecord_model->find_recent_message();
		$this->load->view ( 'home/index',$data);
	}
	/*
	 * 返回商品列表
	 */
	public function get_products(){
		$date = $this->input->post( 'date' );
		$userid = $this->session->userdata ( 'id' );
		$this->load->model ( 'user_model' );
		$user = $this->user_model->load($userid);
		$this->load->model ( 'product_model' );
		$products = $this->product_model->find_by_school_date($user['school'],$date);
		$this->load->model ( 'shoppingcart_model' );
		if(!empty($products)){
			foreach($products as $key=>$product){//添加商品的购买数量
				$products[$key]['amount']=0;
				$temp = $this->shoppingcart_model->find_by_user_product_date ( $userid,$product['id'],$date );
				if(!empty($temp)){
					$products[$key]['amount']=$temp['amount'];
				}
			}
		}
		return_json_data($products);
	}
	/*
	 * 返回商品详细信息
	 */
	public function get_productdetail(){
		$id = $this->uri->segment ( 4 );
		$date = $this->uri->segment ( 6 );
		$this->load->model ( 'product_model' );
		$product = $this->product_model->load($id);
		$userid = $this->session->userdata ( 'id' );
		$this->load->model ( 'shoppingcart_model' );
		$temp = $this->shoppingcart_model->find_by_user_product_date ( $userid,$id,$date );
		$product['amount']=0;
		if(!empty($temp)){
			$product['amount']=$temp['amount'];
		}
		$this->load->model ( 'comment_model' );
		$comment=$this->comment_model->get_avg_score($id);
		if($comment['score']==0){
			$product['score'] = 5;
		}else{
			$product['score'] = round($comment['score']);
		}
		$this->load->model ( 'compose_model' );
		$materials = $this->compose_model->find_by_product($id);
		$nutrient=$this->compose_model->count_by_product($id);
		$data['total'] =$this->get_amount($userid);
		$data['product'] =$product;
		$data['materials'] =$materials;
		$data['nutrient'] =$nutrient;
		$data['date'] =$date;
		$this->load->view ( 'home/detail',$data);
	}
	/*
	 * 返回通知列表
	 */
	public function getMessage(){
		$page = $this->input->post ( 'page' );
		$offset = $this->input->post ( 'offset' );
		$this->load->model ( 'messagerecord_model' );
		$messages = $this->messagerecord_model->find_by_page ($page,$offset);
		return_json(TRUE,$messages,'返回成功！');
	}
	/*
	 * 返回消息详情
	 */
	public function messageDetail() {
		$id = $this->uri->segment(4);
		$this->load->model ( 'messagerecord_model' );
		$message = $this->messagerecord_model->load ($id);
		//$this->load->view ( 'home/messageDetail', $message );
		return_json(TRUE,$message,'返回成功！');

	}
	/*
	 * 返回商品评论信息(待完善)
	 */
	public function get_productcomment(){
		$productid = $this->input->get( 'productid' );
		$page= $this->input->get ( 'page' );
		$offset= $this->input->get ( 'offset' );
		$this->load->model ( 'user_model' );
		$this->load->model ( 'comment_model' );
		$comments = $this->comment_model->find_by_product($productid,$page,$offset);
		foreach($comments as $key=>$comment){//添加用户手机号
			$user=$this->user_model->load($comment['userid']);
			if(!empty($user['phone'])){
				$comments[$key]['name']='*******'.substr($user['phone'],7);
			}
		}
		return_json_data($comments);
	}
	/*
	 * 返回用户购物车内商品的数量
	 */
	private function get_amount($userid) {
		$this->load->model ( 'shoppingcart_model' );
		$carts = $this->shoppingcart_model->find_by_user ( $userid );
		return count($carts);
	}
}
